def load_from_files(files):
	urls=[]
	for fName in files:
		f=open(fName)
		for line in f.xreadlines():
			if not line:break
			line=line.strip()
			if not line:continue
			urls.append(line)
		f.close()	
	return urls		


class _UrlItem:
	def __init__(self,entry,**keyargs):
		for attr,val in entry.iteritems():
			setattr(self,attr,val)
		for attr,val in keyargs.iteritems():
			setattr(self,attr,val)
	def to_entry(self):
		return self.__dict__.copy()
		
class FormatFileLoader:
	UrlItem=_UrlItem
	def __init__(self,fname):
				
		self.fname=fname
		self.f=f=open(fname,'rb')
		line=f.readline()[:-1]
		self.delim=delim=line

		line=f.readline().strip()
		attrs=list(line.split(delim))
		self.uAttrs=attrs
		print attrs

		line=f.readline().strip()
		type_tokens=line.split(delim)
		print type_tokens 
		attrtypes=[]
		for type_token in type_tokens:
			_type=__builtins__[type_token]
			attrtypes.append(_type)
		self.uAttrTypes=attrtypes

	def load(self):
		urls=[]
		f=self.f
		delim=self.delim
		attrs=self.uAttrs
		types=self.uAttrTypes
		lines=f.readlines()
		for line in lines:
			line=line.strip()
			r={}
			vals=line.split(delim)	
			for attr,val,_type in zip(attrs,vals,types):
				if val=='None':
					r[attr]=_type()
				else:
					r[attr]=_type(val)

			urls.append(r)
		f.close()
		return urls
					
			

def test_FormatFileLoader():
        loader=FormatFileLoader('test.txt')
        urls=loader.load()
        for entry in urls:
                print entry
		item=FormatFileLoader.UrlItem(entry,other=3)
		print item.strURL,item.time,item.other
		print item.to_tuple()
if __name__=='__main__':
        test_FormatFileLoader()
		
